import _Array$find from "babel-runtime/core-js/array/find";
export default function createHandlersMiddleware() {
  var handlers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  return function (middlewareAPI) {
    return function (next) {
      return function (action) {
        var _action$meta = action.meta,
            meta = _action$meta === undefined ? {} : _action$meta;

        var actionHandler = _Array$find(handlers.concat(meta.handlers || []), function (h) {
          return h.type === action.type;
        });
        var beforeHandler = actionHandler && actionHandler.beforeHandler;
        var afterHandler = actionHandler && actionHandler.afterHandler;

        if (beforeHandler) {
          beforeHandler(middlewareAPI, action);
        }

        var result = next(action);

        if (afterHandler) {
          afterHandler(middlewareAPI, action);
        }

        return result;
      };
    };
  };
}